package net.entelijan.cobean.examples.showcase.table;

import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.util.ArrayList;
import java.util.List;

import javax.swing.JOptionPane;

import net.entelijan.cobean.core.ColumnDesc;
import net.entelijan.cobean.core.IMultiSelectionListCobean;
import net.entelijan.cobean.core.SelectionMode;
import net.entelijan.cobean.core.TextComponentActionMode;
import net.entelijan.cobean.data.literature.ILitService;
import net.entelijan.cobean.data.literature.LitAuthor;
import net.entelijan.cobean.examples.util.ExamplesUtil;
import net.entelijan.cobean.table.DefaultTableInitializer;
import net.entelijan.cobean.util.CobeanUtil;
import net.entelijan.cobean.util.DateConverter;
import net.entelijan.scf.IShowcase;

public class FilterActionTableShowcase implements
		IShowcase<FilterActionTablePanel> {

	private ILitService service = null;

	public FilterActionTableShowcase() {
		super();
	}

	@Override
	public void init(final FilterActionTablePanel panel) {

		// Create an initializer
		DefaultTableInitializer<LitAuthor> ini = new DefaultTableInitializer<LitAuthor>();

		// Add the 'real' components to the initializer.
		// Convention: Component properties always start with co.. like coTable
		// or coFilterInput
		ini.setCoTable(panel.getTable());
		ini.setCoFilterInput(panel.getFilterTextField());
		ini.setCoFilterButton(panel.getFilterButton());

		// Configure the behaviour of your components.
		ini.setColumnDescs(createColumnDescriptions());
		ini.setFilterActionMode(TextComponentActionMode.ON_ACTION);
		ini.setSelectionMode(SelectionMode.MULTIPLE_INTERVAL);

		// Initialize the components and create your component bean
		// From now the component bean is your interface to the real components.
		final IMultiSelectionListCobean<LitAuthor> cobean = ini.initialize();

		// Add some data to the compnent bean
		final List<LitAuthor> data = service.loadAllAuthors();
		cobean.setValue(data);

		// Now a list of locals is displayed on your table

		panel.getShowSelectionButton().addActionListener(new ActionListener() {

			@Override
			public void actionPerformed(ActionEvent e) {
				JOptionPane.showMessageDialog(panel, ExamplesUtil.listToString(
						cobean.getSelectedValue(), 80));
			}

		});
		// Add an action to the 'remove selected' button
		panel.getDeleteSelectionButton().addActionListener(
				new ActionListener() {

					@Override
					public void actionPerformed(ActionEvent e) {
						CobeanUtil.removeSelected(cobean);
					}

				});

	}

	/**
	 * Create some column descriptions for java.util.Local
	 * 
	 * @return A list of column descriptions
	 */
	private List<ColumnDesc> createColumnDescriptions() {
		List<ColumnDesc> colDescs = new ArrayList<ColumnDesc>();
		{
			ColumnDesc colDesc = new ColumnDesc();
			colDesc.setPropertyName("name");
			colDesc.setHeaderName("Name(filterable)");
			colDesc.setFilterable(true);
			colDescs.add(colDesc);
		}
		{
			ColumnDesc colDesc = new ColumnDesc();
			colDesc.setPropertyName("language");
			colDesc.setHeaderName("Language(sortable)");
			colDesc.setSortable(true);
			colDescs.add(colDesc);
		}
		{
			ColumnDesc colDesc = new ColumnDesc();
			colDesc.setPropertyName("country");
			colDesc.setHeaderName("Country(sortable)");
			colDesc.setSortable(true);
			colDescs.add(colDesc);
		}
		{
			ColumnDesc colDesc = new ColumnDesc();
			colDesc.setPropertyName("birthday");
			colDesc.setHeaderName("Birthday");
			colDesc.setConverter(new DateConverter("yyyy-MM-dd"));
			colDescs.add(colDesc);
		}
		return colDescs;
	}

	public ILitService getService() {
		return service;
	}

	public void setService(ILitService service) {
		this.service = service;
	}

}
